﻿using Kimd.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.Common
{
    public class Mes
    {
        public static string CheckIN(string SN, string TraySN)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.CheckIN.EventID = "SN_CheckIN";
            SysConfig.CheckIN.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.CheckIN.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.CheckIN.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.CheckIN.OPID = SystemConfig.Instance.MesParam.OPID;
            SysConfig.CheckIN.Token = SystemConfig.Instance.MesParam.Token;
            SysConfig.CheckIN.FixSN = SystemConfig.Instance.MesParam.FixSN;
            SysConfig.CheckIN.SN = SN.Replace("\r", "");
            SysConfig.CheckIN.CarrierID = TraySN;
            SysConfig.CheckIN.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");

            string sendstr = JsonConvert.SerializeObject(SysConfig.CheckIN);

            LogHelper.Instance.Log("CheckIn-Send", sendstr, MessageLevel.Mes);
            string result = Mes.Post(SysConfig.URL + "/" + SysConfig.CheckIN.EventID, sendstr);
            LogHelper.Instance.Log("CheckIn-Receive", result, MessageLevel.Mes);
            return result;
        }
        public static Status_Result Status(string status)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.Status.EventID = "Status";
            SysConfig.Status.STATUS = status;
            SysConfig.Status.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.Status.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.Status.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.Status.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            SysConfig.Status.OPID = SystemConfig.Instance.MesParam.OPID;
            SysConfig.Status.Token = SystemConfig.Instance.MesParam.Token;
            SysConfig.Status.FixSN = SystemConfig.Instance.MesParam.FixSN;
            if (!Directory.Exists("D:\\MESLog"))
                Directory.CreateDirectory("D:\\MESLog");
            StreamWriter sw = File.AppendText("D:\\MESLog\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");

            string sendstr = JsonConvert.SerializeObject(SysConfig.Status);
            sw.Write("发送数据：\r\n" + sendstr + "\r\n");
            string result = Mes.Post(SysConfig.URL + "/" + SysConfig.Status.EventID, sendstr);
            sw.Write("接受数据：\r\n" + result + "\r\n");
            sw.Close();
            Status_Result sr = new Status_Result();
            try
            {
                sr = (Status_Result)JsonConvert.DeserializeObject(result, typeof(Status_Result));
            }
            catch
            {
                return sr;
            }
            return sr;
        }
        public static Alarm_Result Alarm(int ERRORID)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.Alarm.EventID = "Alarm";
            SysConfig.Alarm.AlarmID = ERRORID.ToString();
            SysConfig.Alarm.ResetTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            SysConfig.Alarm.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.Alarm.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.Alarm.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.Alarm.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            SysConfig.Alarm.OPID = SystemConfig.Instance.MesParam.OPID;
            string sendstr = JsonConvert.SerializeObject(SysConfig.Alarm);
            //richTextBox1.AppendText("发送数据：\r\n" + sendstr + "\r\n");
            string result = Mes.Post(SysConfig.URL + "/" + SysConfig.Alarm.EventID, sendstr);
            //richTextBox1.AppendText("接受数据：\r\n" + result + "\r\n");
            Alarm_Result sr = new Alarm_Result();
            try
            {
                sr = (Alarm_Result)JsonConvert.DeserializeObject(result, typeof(Alarm_Result));
            }
            catch
            {
                return sr;
            }
            return sr;
        }
        public static string FeedingCheck(string SN)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.FeedingCheck.EventID = "Magz";
            SysConfig.FeedingCheck.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.FeedingCheck.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.FeedingCheck.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.FeedingCheck.OPID = SystemConfig.Instance.MesParam.OPID;
            SysConfig.FeedingCheck.Token = SystemConfig.Instance.MesParam.Token;
            SysConfig.FeedingCheck.FixSN = SystemConfig.Instance.MesParam.FixSN;
            SysConfig.FeedingCheck.SN = SN.Replace("\r", "");
            SysConfig.FeedingCheck.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            
            string sendstr = JsonConvert.SerializeObject(SysConfig.FeedingCheck);

            LogHelper.Instance.Log("FeedingCheck-Send", sendstr, MessageLevel.Mes);
            string result = Mes.Post(SysConfig.URL + "/" + "SN_FeedingCheck", sendstr);
            LogHelper.Instance.Log("FeedingCheck-Receive", result, MessageLevel.Mes);
            return result;
        }
        public static string CarrierBind(string SN, string cavityNo, string carrierSN)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.CarrierBind.EventID = "SN_CarrierBind";
            SysConfig.CarrierBind.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.CarrierBind.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.CarrierBind.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.CarrierBind.OPID = SystemConfig.Instance.MesParam.OPID;
            SysConfig.CarrierBind.Token = SystemConfig.Instance.MesParam.Token;
            SysConfig.CarrierBind.FixSN = SystemConfig.Instance.MesParam.FixSN;
            SysConfig.CarrierBind.SN = SN.Replace("\r", "");
            SysConfig.CarrierBind.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            SysConfig.CarrierBind.BindType = "Bind";
            SysConfig.CarrierBind.CarrierID = carrierSN;
            //1开始
            SysConfig.CarrierBind.ACPoint = cavityNo;

            string sendstr = JsonConvert.SerializeObject(SysConfig.CarrierBind);

            LogHelper.Instance.Log("CarrierBind-Send", sendstr, MessageLevel.Mes);
            string result = Mes.Post(SysConfig.URL + "/" + SysConfig.CarrierBind.EventID, sendstr);
            LogHelper.Instance.Log("CarrierBind-Receive", result, MessageLevel.Mes);
            return result;
        }
        public static string CarrierCheck(string CarrierSN)
        {
            MesConfig SysConfig = new MesConfig();
            SysConfig.URL = SystemConfig.Instance.MesParam.URL;
            SysConfig.CarrierCheck.EventID = "Carrier_Check";
            SysConfig.CarrierCheck.Line = SystemConfig.Instance.MesParam.Line;
            SysConfig.CarrierCheck.StationID = SystemConfig.Instance.MesParam.StationID;
            SysConfig.CarrierCheck.MachineID = SystemConfig.Instance.MesParam.MachineID;
            SysConfig.CarrierCheck.OPID = SystemConfig.Instance.MesParam.OPID;
            SysConfig.CarrierCheck.Token = SystemConfig.Instance.MesParam.Token;
            SysConfig.CarrierCheck.FixSN = SystemConfig.Instance.MesParam.FixSN;
            SysConfig.CarrierCheck.CarrierID = CarrierSN.Replace("\r", "");
            SysConfig.CarrierCheck.SendTime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
           
            string sendstr = JsonConvert.SerializeObject(SysConfig.CarrierCheck);

            LogHelper.Instance.Log("CarrierCheck-Send", sendstr, MessageLevel.Mes);
            string result = Mes.Post(SysConfig.URL + "/" + SysConfig.CarrierCheck.EventID, sendstr);
            LogHelper.Instance.Log("CarrierCheck-Receive", result, MessageLevel.Mes);
            return result;
        }
        public static string Post(string url, string postData, int timeout = 3000)
        {

            try
            {
                //创建一个HTTP请求 
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                //Post请求方式 
                request.Method = "POST";
                //内容类型 
                request.ContentType = "application/json";
                byte[] payload = Encoding.UTF8.GetBytes(postData);
                request.ContentLength = payload.Length;
                Stream writer;
                String strValue = "";//strValue为http响应所返回的字符流 
                try
                {
                    writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                //将请求参数写入流 
                writer.Write(payload, 0, payload.Length);
                writer.Close();//关闭请求流 

                HttpWebResponse response;
                try
                {
                    //获得响应流 
                    request.Timeout = timeout;
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    // response = ex.Response as HttpWebResponse;
                    throw ex;
                }
                Stream s = response.GetResponseStream();
                using (var httpStreamReader = new StreamReader(s, Encoding.GetEncoding("utf-8")))
                {
                    string responseContent = httpStreamReader.ReadToEnd();
                    strValue = responseContent;
                }
                s.Close();
                return strValue;//返回Json数据
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
